#include <stdio.h>

#include <Windows.h>

int window_subprocess(char *outfile, char *infile, char *exe)
{
    PROCESS_INFORMATION pi;
	STARTUPINFO si = {sizeof(STARTUPINFO)};
	SECURITY_ATTRIBUTES sa = {sizeof(SECURITY_ATTRIBUTES),NULL,TRUE};
	HANDLE cmdOutput = CreateFile(outfile,
						GENERIC_WRITE,
						FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
						&sa,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(cmdOutput == INVALID_HANDLE_VALUE)
	{
		ExitProcess(0);
	}
 
	HANDLE cmdInput = CreateFile(infile,
						GENERIC_READ,
						FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE,
						&sa,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(cmdInput == INVALID_HANDLE_VALUE)
	{
		ExitProcess(0);
	}
 
	si.hStdInput = cmdInput;
	si.hStdOutput = cmdOutput;
    si.hStdError = cmdOutput;
	si.dwFlags = STARTF_USESTDHANDLES;
    DWORD flags = CREATE_NO_WINDOW;

	if(CreateProcess(NULL, exe,NULL,NULL,TRUE,flags,NULL,NULL,&si,&pi))
	{
        // 等待退出
		//WaitForSingleObject(pi.hProcess,INFINITE);
		CloseHandle(pi.hProcess);
		CloseHandle(pi.hThread);
	}
 
	CloseHandle(cmdOutput);
	CloseHandle(cmdInput);
 
	return 0;
}

int main()
{
    int a;
    window_subprocess("output.txt", "input.txt", "test.exe");
    printf("Hello World \n");
}